﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{
    public class Radio : System.Windows.Forms.UserControl
    {
        public Radio()
        {
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.ResizeRedraw, true); //尺寸变化后绘制
            SetStyle(ControlStyles.Opaque, false); //改善绘图质量
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);

        }

        private int progress;
        private bool check;

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            RectangleF rectangle = new RectangleF(0, 0, 14, 14);

            Color color = Color.White;
            if (this.check)
                color = Color.FromArgb(66, 158, 251);
            using (Brush brush = new SolidBrush(color))
            {
                e.Graphics.FillEllipse(brush, rectangle);
                if (this.check && progress > 0)
                {
                    rectangle = new RectangleF(rectangle.X + (rectangle.Width - progress) / 2, rectangle.Y + (rectangle.Height - progress) / 2, progress, progress);
                    e.Graphics.FillEllipse(Brushes.White, rectangle);
                }
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            check = !check;
            progress = 0;
            this.Invalidate();
            Application.DoEvents();
            if (check)
            {
                for (int i = 1; i < 7; i++)
                {
                    progress = i;
                    Action action = () =>
                    {
                        this.Invalidate();
                        Application.DoEvents();
                        Thread.Sleep(15);
                    };
                    this.BeginInvoke(action);

                }
            }
        }
    }
}
